<?php

namespace app\api\middleware;

use app\api\controller\Base;
use think\facade\Db;

class CheckLog extends Base
{
    public function handle($request, \Closure $next)
    {
        if(getConfig('admin','sys_main')['app_switch'] == 0)
        {
            json_msg('App正在升级，请稍后再来尝试');
        }
        $token = input('token');
        if (!$token) {
            $data = ['msg' => 'token不能为空', 'data' => [], 'code' => 0];
            exit(json_encode($data, JSON_UNESCAPED_UNICODE));
        }
        $user = cache($token);
        if (!$user) {
            $data = ['msg' => 'token不存在或已过期', 'data' => [], 'code' => 0];
            exit(json_encode($data, JSON_UNESCAPED_UNICODE));
        }
        $userData = json_decode($user, true);
        $this->uid = $userData['id'];
        $this->checkStatus();

        return $next($request);
    }
    public function checkStatus()
    {
        $check = Db::name('user')->where(['id' => $this->uid])->find();
        if($check['status'] == 0) json_msg('用户待审核');
        if($check['status'] == 2) json_msg('账户已被封禁');
    }
}
